﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mesoBoard.Data.Repositories
{
    public class PluginConfigRepository : IPluginConfigRepository
    {
        private mbEntities db;

        public PluginConfigRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public void CreatePluginConfig(PluginConfig config)
        {
            db.PluginConfigs.AddObject(config);
            db.SaveChanges();
        }

        public void CreatePluginConfigs(IEnumerable<PluginConfig> configs)
        {
            foreach (var c in configs)
            {
                db.PluginConfigs.AddObject(c);
            }

            db.SaveChanges();
        }

        public void UpdatePluginConfig(string name, string value)
        {
            db.PluginConfigs.FirstOrDefault(x => x.Name == name).Value = value;
            db.SaveChanges();
        }

        public IEnumerable<PluginConfig> PluginConfigs()
        {
            return db.PluginConfigs.AsEnumerable();
        }

        public string Value(string Name)
        {
            return db.PluginConfigs.FirstOrDefault(x => x.Name == Name).Value;
        }

        public bool IsPluginConfig(string Name)
        {
            return db.PluginConfigs.ToList().Exists(x => x.Name == Name);
        }

        public void BulkUpdate(Dictionary<string, string> Vals)
        {
            foreach (var v in Vals)
            {
                db.PluginConfigs.First(x => x.Name == v.Key).Value = v.Value;
            }
            db.SaveChanges();
        }
    }
}
